<?php
/**
 * XMl-Routenanalyse
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2013 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 * @TODO es koennte noch ein caching-problem bestehen
 * */


Class Yapary_Route_XmlRoute
{
	private $oSimpleXmlElement;


    /**
     * @param string $_xml	: XML
     * */
    function __construct ($_xml) {
    	try {
			$this->oSimpleXmlElement	= simplexml_load_string($_xml);
    	} catch (Exception $e) {
    		throw $e;
    	}
    }


    /**
     * Liefert anhand des Controllers die Url
     * @param string $Controller 	: Name des Controllers
     * @return string
     * */
    public function getUrl ($_controller, array $arrParam = array()) {
    	$xPath		= "/root/route";
    	$arrResult	= $this->oSimpleXmlElement->xpath($xPath);

    	foreach ($arrResult as $key => $arrVal) {
    		$grep			= (string) $arrVal->grep;
    		$regex			= (string) $arrVal->regex;
    		$controller		= (string) $arrVal->controller;
    		$vars			= $arrVal->vars;

    		if ($controller == $_controller) {
    			if (count($arrParam)) {
    				foreach ($arrParam as $key => $val) {
    					$grep = str_replace('$'.$key, $val, $grep);
    				}
    			}
       			if (count(preg_grep($regex, array($grep))) ) {
					return $grep;
    			} else {
    				// Diese Warning tritt auf, wenn das Suchmuster $grep nicht zum regulaere Ausdruck $regex passt
    				trigger_error('Invalid preg_grep for controller: '.$controller.' grep: '.$grep.' on regex: '.$regex, E_USER_WARNING);
    			}

    		}
    	}

    	return NULL;
	}


	/**
	 * Liefert anhand des Controllers die Url
	 * @param string $Controller 	: Name des Controllers
	 * @return string
	 * */
	public function getUrlParam ($_controller) {
		$xPath		= "/root/route";
		$arrResult	= $this->oSimpleXmlElement->xpath($xPath);

		foreach ($arrResult as $key => $arrVal) {
			//    		echo '<pre>'.print_r($arrVal, true).'</pre>';
			$grep			= (string) $arrVal->grep;
			$regex			= (string) $arrVal->regex;
			$controller		= (string) $arrVal->controller;
			$vars			= $arrVal->vars;

			if ($controller == $_controller) {
				$arrParam	= explode('/', $grep);
				if (count($arrParam)) {
					// Entferne von $arrRoute alle anfangseintraege so lange, bis der Erste Parameter von $arrParam auf den ersten Parameter von $arrRoute passt
					$arrRoute			= explode('/', $_SERVER['REQUEST_URI']);
					$paramFirstEntry	= $arrParam[0];
					foreach ($arrRoute as $key => $val) {
						if ($val != $paramFirstEntry) {
							unset($arrRoute[$key]);
						} else { // Breche nach dem ersten Fund ab, da ab nun an die Felder gleich sein sollten
							reset($arrRoute);
							break;
						}
					}

					// Verfuegt nach diesem Durchlauf $arrRoute nicht gleich viele Eintraege wie $arrParam, passt etwas nicht
					if (count($arrRoute) != count($arrParam)) {
						trigger_error('Invalid preg_grep in controller: '.$controller.' grep: '.$grep.'. Something is wrong', E_USER_WARNING);
					}

					foreach ($arrParam as $key => $val) {
						$arrParam[$key] = current($arrRoute);
						next($arrRoute);
					}
				} else {
					trigger_error('Invalid preg_grep in controller: '.$controller.' grep: '.$grep.'. Missing arrParam() to Validate grep', E_USER_WARNING);
					return NULL;
				}

				if (count(preg_grep($regex, array(implode('/', $arrParam)))) ) {
					return $arrParam;
				} else {
					// Diese Warning tritt auf, wenn das Suchmuster $grep nicht zum regulaere Ausdruck $regex passt
					trigger_error('Invalid preg_grep for controller: '.$controller.' grep: '.$grep, E_USER_WARNING);
					return NULL;
				}

			}
		}

		return NULL;
	}


    /**
     * @desc Liefert anhand einer Url den Namen des Controllers
     * @param string $url 	: Uri-String
     * @return string
     * */
    public function getController ($_url) {
		$xPath		= "/root/route";
		$arrResult	= $this->oSimpleXmlElement->xpath($xPath);

		foreach ($arrResult as $key => $arrVal) {
			//echo '<pre>'.print_r($arrVal, true).'</pre>';
    		$grep			= (string) $arrVal->grep;
			$regex			= (string) $arrVal->regex;
			$controller		= (string) $arrVal->controller;
			$vars			= $arrVal->vars;

			if (preg_match($regex, $_url, $arrMatches)) {
				return $controller;
			}
		}

		return NULL;
    }


    /**
     * @desc Liefert anhand eines Controllers dessen Parameter
     * @param string $url 	: Uri-String
     * @return string
     * */
    public function getListVars ($controller) {
		$arrResult	= array();
		$xPath		= '/root/route/controller[. = "'.$controller.'"]/parent::*/vars/var';
		$arr		= $this->oSimpleXmlElement->xpath($xPath);

		if (count($arr)) {
			foreach ($arr as $key => $arrVal) {
	    		$name			= (string) $arrVal->name;
				$value			= (string) $arrVal->value;

				$arrResult[$name] = $value;
			}
		}

		return $arrResult;
	}


    /**
     * Liefert die Anzahl vorhandener Routen
     * @return int
     * */
    public function getCount () {
    	$xPath		= "/root/route";
    	$arrResult	= $this->oSimpleXmlElement->xpath($xPath);

    	return count($arrResult);
    }

}


?>